
//  var a=JSON.parse(sessionStorage.getItem("imp"));
var a = JSON.parse(sessionStorage.getItem("imp"));
var lis = document.querySelectorAll("li div");
var h3 = document.querySelector("h3");
var li = document.querySelectorAll("li");
var btn = document.querySelector(".button");
var btn1 = document.querySelector(".button1");
var btn2 = document.querySelector(".button2");
var btn3 = document.querySelector(".button3");
var btn4 = document.querySelector(".button4");
var btn5 = document.querySelector(".button5");
var data = document.querySelector(".data");
var ul = document.querySelector("ul");
var number = document.querySelector(".number");
var kill = document.querySelector(".kill");
var show = document.querySelector(".show");
var num = 0;
var yewan = 1;

for (var i = 0; i < lis.length; i++) {
    lis[i].innerHTML = a[i + 1];
}
//身份：女巫  第几晚被杀  救了谁 毒了谁
//预言家  第几晚被杀  查看了谁
//狼人
var peoples = {//9个人各自抽象为对象
    1: {

    },
    2: {

    },
    3: {

    },
    4: {

    },
    5: {

    },
    6: {

    },
    7: {

    },
    8: {

    },
    9: {

    },
}
//初始化
for (var item in a) {
    peoples[item][0] = a[item];
    peoples[item]["life"] = 1;
    peoples[item][yewan] = [];

}
for (var item in peoples) {
    if (peoples[item][0] == "女巫") {
        peoples[item]["毒药"] = 1;
        peoples[item]["解药"] = 1;
    }
}


//如果选中 
function select() {
    for (var i = 0; i < li.length; i++) {
        li[i].addEventListener("click", function () {
            for (var j = 0; j < li.length; j++) {
                li[j].style.backgroundColor = "#F5C97B";
            }
            this.style.backgroundColor = "grey";

        })

    }
}

function news() {
    for (var i = 0; i < li.length; i++) {
        if(li[i].style.backgroundColor==="green"){
           continue;
        }
        else{
        li[i].style.backgroundColor = "#F5C97B";
        }
    }
}
//狼人杀人方法
function wolf() {

    var weishu=1;
    for (var i = 0; i < li.length; i++) {
        if (li[i].style.backgroundColor === "grey") {
            if(peoples[i+1][0]=="女巫"){
                break;
            }
            else{
            peoples[i + 1]["life"] = 0;
            li[i].style.cursor="not-allowed";
            }
             weishu = li[i].getAttribute("index");
            // li[weishu-1].style.backgroundColor="green";       
        }
        
    }
    console.log(peoples);

    return weishu;

}



select();
btn.addEventListener("click", function () {
    wolf();
    var weishu = wolf();
    console.log(weishu);
    console.log(yewan);
    btn1.setAttribute("index", 0);
    btn2.setAttribute("index", 0);
    btn3.setAttribute("index", 0);
    
    
    peoples[weishu][yewan].push("被狼人杀");
    console.log(peoples[1]);
    
    jieyao(weishu);
   
    for (var item in peoples) {
        if (peoples[item][0] == "女巫") {
            if (peoples[item]["毒药"] == 1&&peoples[item]["life"]==1 ) {
                var a = confirm("女巫是否要毒？");
                console.log(a);
                
                if (a == 1) {
                    news();
                    btn3.style.display = "block";
                    btn.style.display = "none";
                    btn2.style.display = "none";
                    peoples[item]["毒药"] = 0;
       

                }
                else {
                    yuyan();
                }
            }
            else{
                yuyan();
            }

        }
    }



})


btn3.addEventListener("click", function () {

    wolf();
    var zuowei = wolf();
    peoples[zuowei][yewan].push("被毒死");
    btn3.style.display = "none";
    yuyan();
    isover();
})

btn2.addEventListener("click", function () {
    for (var i = 0; i < li.length; i++) {
        if (li[i].style.backgroundColor == "grey") {
            peoples[i + 1][yewan].push("被预言家查看");
            console.log(peoples);
            
            tou();
            

        }
    }
})
btn1.addEventListener("click", function () {
    wolf();
    var weishu = wolf();
    peoples[weishu][yewan].push("被投死");
   
    number.innerHTML = '' + yewan + '';
 
    btn1.style.display = "none";
    btn.style.display = "none";
    btn5.style.display = "block";
    ul.style.display="none";
    data.style.display="block";
    kill.innerHTML="第"+yewan+"晚";

    show.innerHTML=rizhi();
    console.log(yewan);
   

   
    isover();


})
btn5.addEventListener("click",function(){
    yewan = yewan + 1;
    for (var item in peoples) {
        peoples[item][yewan] = [];

    }
    number.innerHTML = '' + yewan + '';
    news();
    btn5.style.display="none";
    btn.style.display="block";
    data.style.display="none";
    ul.style.display="flex";
})
function isover() {
    var lang = [];
    var shen = [];
    var min = [];
    for (var item in peoples) {

        console.log(peoples[item][0], peoples[item]["life"]);

        if (peoples[item][0] == "狼人" && peoples[item]["life"] == 0) {
            lang.push("狼人");

        }
        if (peoples[item][0] == "平民" && peoples[item]["life"] == 0) {
            min.push("平民");
        }
        if ((peoples[item][0] == "预言家" || peoples[item][0] == "女巫" || peoples[item][0] == "猎人") && peoples[item]["life"] == 0) {
            shen.push("神");
        }


    }
    if (lang.length == 3) {
        ul.style.display = "none";
        data.style.display = "block";
        kill.innerHTML = "平民获胜";
        show.innerHTML = bianli();
        btn.style.display="none";
        btn1.style.display="none";
        btn2.style.display="none";
        btn3.style.display="none";
        btn4.style.display="none";
        btn5.style.display="none";

    }
    if (shen.length == 3 || min.length == 3) {
        ul.style.display = "none";
        data.style.display = "block";
        kill.innerHTML = "狼人获胜";
        show.innerHTML = bianli();
        btn.style.display="none";
        btn1.style.display="none";
        btn2.style.display="none";
        btn3.style.display="none";
        btn4.style.display="none";
        btn5.style.display="none";
    }
}
//投票
function tou() {
    news();
    btn1.style.display = "block";
    btn.style.display = "none";
    btn2.style.display = "none";
    btn3.style.display = "none";

}

//预言家
function yuyan() {
    for (var item in peoples) {
        if (peoples[item][0] == "预言家") {
            if (peoples[item]["life"] == 1) {
                news();
                btn2.style.display = "block";
                btn.style.display = "none";
                btn1.style.display = "none";
                btn3.style.display = "none";
         
            }
            else {
                tou();
            }
        }
    }
}
function jinyong(){
    for(var item in peoples){
        if(peoples[item]["life"]==0){
            li[item-1].style.display="green";
        }
    }
}
//女巫jie
function jieyao(zuowei) {
    console.log(zuowei);

    for (var item in peoples) {
        console.log(peoples[item]);

        if (peoples[item][0] == "女巫") {
            console.log(11);

            if (peoples[item]["解药"] == 1&&peoples[item]["life"]==1) {
                var a = confirm("女巫是否要救?");
                if (a == 1) {
                    peoples[item]["解药"] = 0;
                    peoples[zuowei]["life"] = 1;
                    li[zuowei-1].style.backgroundColor="#F5C97B";
                    peoples[zuowei][yewan].push("被解救");
                    console.log(peoples);

                }
                else {
                    if(peoples[zuowei][0]=="女巫"){
                        peoples[zuowei]["life"]=0;
                        li[zuowei-1].style.cursor="not-allowed";
                    }
                }
            }
        }
    }
}

function bianli() {//最后输出总的日志
    a = [];
    console.log(yewan);

    for (var i = 1; i <=yewan; i++) {
        for (var item in peoples) {
            console.log(peoples[item][i]);
            if (peoples[item][i].length >= 1) {
                a.push('第' + i + '晚', item + "号"+ peoples[item][i]+"<br>");
            }
        }
    }
    return a;
}
function rizhi(){//对应的每天日志
    a=[];
    for(var item in peoples){
        if (peoples[item][yewan].length >= 1) {
            a.push('第' + yewan + '晚', item + "号"+peoples[item][yewan]+"<br>");
        }
    }
    return a;
}